package com.cskaoyan.service.impl;

import com.cskaoyan.bean.bo.WxCommentCountBo;
import com.cskaoyan.bean.bo.WxCommentListBo;
import com.cskaoyan.bean.bo.WxCommentPostBo;
import com.cskaoyan.bean.pojo.MallComment;
import com.cskaoyan.bean.pojo.MallCommentExample;
import com.cskaoyan.bean.pojo.MallUser;

import com.cskaoyan.bean.vo.*;
import com.cskaoyan.mapper.MallCommentMapper;
import com.cskaoyan.mapper.MallGoodsMapper;
import com.cskaoyan.mapper.MallUserMapper;
import com.cskaoyan.service.WxCommentListService;
import com.github.pagehelper.PageHelper;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Description:
 * @Author: chaohe@com.cskaoyan
 * @Date: 2021/7/12
 * @Time: 19:22
 */
@Service
public class WxCommentListServiceImpl implements WxCommentListService {

    @Autowired
    MallGoodsMapper mallGoodsMapper;

    @Autowired
    MallCommentMapper mallCommentMapper;

    @Autowired
    MallUserMapper mallUserMapper;
    /**
     * @Author chaohe
     * @description: 商品品论的列表
     * @Date 9:04 2021/7/13
     * url:  http://localhost:8083/wx/comment/list
     */

    @Override
    public WxCommentListVOO list(WxCommentListBo wxCommentListBo) {


        PageHelper.startPage(wxCommentListBo.getPage(), wxCommentListBo.getSize());

        WxCommentListVOO wxCommentListVO = new WxCommentListVOO();
        if (wxCommentListBo.getShowType() == 0 && wxCommentListBo.getType() == 0) {

            MallCommentExample mallCommentExample = new MallCommentExample();
            MallCommentExample.Criteria criteria = mallCommentExample.createCriteria();
            criteria.andValueIdEqualTo(wxCommentListBo.getValueId());

            mallCommentExample.setOrderByClause("add_time"+" "+"desc");
            List<MallComment> mallComments = mallCommentMapper.selectByExample(mallCommentExample);
            List<WxCommentListVo1> wxCommentListVo1s = new ArrayList<>();
            for (MallComment mallComment : mallComments) {

                MallUser mallUser = mallUserMapper.selectByPrimaryKey(mallComment.getUserId());

                WxCommentListVo2 wxCommentListVo2 = new WxCommentListVo2(mallUser.getNickname(), mallUser.getAvatar());

                WxCommentListVo1 wxCommentListVo1 = new WxCommentListVo1();
                wxCommentListVo1.setUserInfo(wxCommentListVo2);

                wxCommentListVo1.setAddTime(mallComment.getAddTime());
                wxCommentListVo1.setContent(mallComment.getContent());
                String[] picUrls = mallComment.getPicUrls();
                wxCommentListVo1.setPicList(picUrls);

                wxCommentListVo1s.add(wxCommentListVo1);


            }
            wxCommentListVO.setData(wxCommentListVo1s);


            return wxCommentListVO;
        }
        if (wxCommentListBo.getShowType() == 1 && wxCommentListBo.getType() == 0) {

            MallCommentExample mallCommentExample = new MallCommentExample();
            MallCommentExample.Criteria criteria = mallCommentExample.createCriteria();
            criteria.andValueIdEqualTo(wxCommentListBo.getValueId());
            criteria.andHasPictureEqualTo(true);

            mallCommentExample.setOrderByClause("add_time"+" "+"desc");
            List<MallComment> mallComments = mallCommentMapper.selectByExample(mallCommentExample);

            List<WxCommentListVo1> wxCommentListVo1s = new ArrayList<>();
            for (MallComment mallComment : mallComments) {

                MallUser mallUser = mallUserMapper.selectByPrimaryKey(mallComment.getUserId());

                WxCommentListVo2 wxCommentListVo2 = new WxCommentListVo2(mallUser.getNickname(), mallUser.getAvatar());

                WxCommentListVo1 wxCommentListVo1 = new WxCommentListVo1();
                wxCommentListVo1.setUserInfo(wxCommentListVo2);

                wxCommentListVo1.setAddTime(mallComment.getAddTime());
                wxCommentListVo1.setContent(mallComment.getContent());
                String[] picUrls = mallComment.getPicUrls();
                wxCommentListVo1.setPicList(picUrls);

                wxCommentListVo1s.add(wxCommentListVo1);


            }
            wxCommentListVO.setData(wxCommentListVo1s);

            return wxCommentListVO;
        }
        if (wxCommentListBo.getShowType() == 0&& wxCommentListBo.getType() == 1) {

            MallCommentExample mallCommentExample = new MallCommentExample();
            MallCommentExample.Criteria criteria = mallCommentExample.createCriteria();
            criteria.andValueIdEqualTo(wxCommentListBo.getValueId());
            criteria.andHasPictureEqualTo(true);

            mallCommentExample.setOrderByClause("add_time"+" "+"desc");
            List<MallComment> mallComments = mallCommentMapper.selectByExample(mallCommentExample);

            List<WxCommentListVo1> wxCommentListVo1s = new ArrayList<>();
            for (MallComment mallComment : mallComments) {

                MallUser mallUser = mallUserMapper.selectByPrimaryKey(mallComment.getUserId());

                WxCommentListVo2 wxCommentListVo2 = new WxCommentListVo2(mallUser.getNickname(), mallUser.getAvatar());

                WxCommentListVo1 wxCommentListVo1 = new WxCommentListVo1();
                wxCommentListVo1.setUserInfo(wxCommentListVo2);

                wxCommentListVo1.setAddTime(mallComment.getAddTime());
                wxCommentListVo1.setContent(mallComment.getContent());
                String[] picUrls = mallComment.getPicUrls();
                wxCommentListVo1.setPicList(picUrls);

                wxCommentListVo1s.add(wxCommentListVo1);


            }
            wxCommentListVO.setData(wxCommentListVo1s);

            return wxCommentListVO;
        }
        return wxCommentListVO;
    }
    /**
     * @Author chaohe
     * @description: 评论的总数
     * @Date 9:05 2021/7/13
     * url:   http://localhost:8083/wx/comment/count
     */

    @Override
    public WxCommentCountVo count(WxCommentCountBo wxCommentCountBo) {

        MallCommentExample mallCommentExample = new MallCommentExample();
        MallCommentExample.Criteria criteria = mallCommentExample.createCriteria();

        criteria.andValueIdEqualTo(wxCommentCountBo.getValueId());
        int l = (int) mallCommentMapper.countByExample(mallCommentExample);

        criteria.andHasPictureEqualTo(true);
        int l1 = (int) mallCommentMapper.countByExample(mallCommentExample);

        WxCommentCountVo wxCommentCountVo = new WxCommentCountVo(l1, l);


        return wxCommentCountVo;
    }

    @Override
    public WxCommentPostVo post(WxCommentPostBo wxCommentPostBo) {
        Subject subject = SecurityUtils.getSubject();
        MallUser mallUser = (MallUser) subject.getPrincipal();
        Date date = new Date();
        Timestamp timestamp = new Timestamp(date.getTime());

        int userId = mallUser.getId();
        MallComment mallComment = new MallComment(null, wxCommentPostBo.getValueId(), wxCommentPostBo.getType(), wxCommentPostBo.getContent(), userId, wxCommentPostBo.getHasPicture(), wxCommentPostBo.getPicUrls(), wxCommentPostBo.getStar(), timestamp, timestamp, false);

        int i = mallCommentMapper.insertSelective(mallComment);

        if (i == 1) {
            MallComment mallComment1 = mallCommentMapper.selectByPrimaryKey(mallComment.getId());

            WxCommentPostVo wxCommentPostVo = new WxCommentPostVo(mallComment1.getId(), mallComment1.getValueId(), mallComment1.getType(), mallComment1.getContent(), mallComment1.getUserId(), mallComment1.getHasPicture(), mallComment1.getPicUrls(), mallComment1.getStar(), mallComment1.getAddTime(), mallComment1.getUpdateTime());

            return wxCommentPostVo;
        }




        return null;
    }
}

